//
//  UIButton+Click.m
//  RuntimeDemo
//
//  Created by zxs on 2017/6/14.
//  Copyright © 2017年 zxs. All rights reserved.
//

#import "UIButton+Click.h"
#import <objc/runtime.h>

static const void *associatedKey = @"associatedKey";

@implementation UIButton (Click)

- (void)setClick:(clickBlock)click{
    objc_setAssociatedObject(self, associatedKey, click, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self removeTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    if (click) {
        [self addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (clickBlock)click {
    return objc_getAssociatedObject(self, associatedKey);
}

- (void)buttonClick {
    if (self.click) {
        self.click();
    }
}

@end
